package com.vvlive.caibao.smartcity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.vvlive.caibao.smartcity.bean.Notification;
import com.vvlive.caibao.smartcity.utils.ScreenUtil;
import com.vvlive.caibao.smartcity.view.RoundImageView;

import org.kymjs.kjframe.KJBitmap;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

import static android.support.v7.widget.RecyclerView.Adapter;
import static android.support.v7.widget.RecyclerView.ViewHolder;

/**
 * Created by zhj on 2015/7/22.
 */
public class NotificationRecyclerAdapter extends Adapter implements OnClickListener {

    private static final int DEFAULT_MARGIN_LEFT_PX = 16;
    private static final int DEFAULT_MARGIN_TOP_PX = 8;
    private static final int DEFAULT_MARGIN_RIGHT_PX = 16;
    private static final int DEFAULT_MARGIN_BOTTOM_PX = 0;

    private int marginLeft, marginTop, marginRight, marginBottom;

    private Context context;
    private List<Notification> list;
    private KJBitmap mKJBitmap;
    private OnItemClickListener onItemClickListener;
    private ScreenUtil screenUtil;
    private LayoutParams layoutParams;

    @SuppressLint("NewApi")
    public NotificationRecyclerAdapter(Context context, List<Notification> list) {
        this.context = context;
        this.list = list;
        mKJBitmap = new KJBitmap();
        screenUtil = new ScreenUtil(context);
        layoutParams = new LayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        marginLeft = (int) screenUtil.dip2px(DEFAULT_MARGIN_LEFT_PX);
        marginTop = (int) screenUtil.dip2px(DEFAULT_MARGIN_TOP_PX);
        marginRight = (int) screenUtil.dip2px(DEFAULT_MARGIN_RIGHT_PX);
        marginBottom = (int) screenUtil.dip2px(DEFAULT_MARGIN_BOTTOM_PX);
        layoutParams.setMargins(marginLeft, marginTop, marginRight, marginBottom);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_notification_recycler, null, false);
        itemView.setLayoutParams(layoutParams);
        return new NotificationViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Notification notification = list.get(position);
        NotificationViewHolder viewHolder = (NotificationViewHolder) holder;
        viewHolder.title.setText(notification.getTitle());
        viewHolder.shortMessage.setText(notification.getShortMessage() +"\n" +notification.getShortMessage() +"\n" + notification.getShortMessage());
        viewHolder.itemView.setTag(position);
        viewHolder.itemView.setOnClickListener(this);
        viewHolder.more.setTag(notification);
        viewHolder.more.setOnClickListener(this);
        viewHolder.del.setTag(notification);
        viewHolder.del.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.noti_msg_more:
                Toast.makeText(context, "没有更多...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.noti_msg_del:
                Object target = v.getTag();
                for(int i = 0; i < list.size(); i++){
                    if(list.get(i).equals(target)){
                        this.notifyItemRemoved(i);
                        break;
                    }
                }
                list.remove(target);
                break;
            default:
                if (onItemClickListener != null){
                    onItemClickListener.onItemClick(v, (int) v.getTag());
                }
                break;
        }

    }

    public interface OnItemClickListener{
        void onItemClick(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * ViewHolder
     */
    private final class NotificationViewHolder extends ViewHolder{
        public View itemView;
        public RoundImageView icon;
        public TextView title;
        public TextView shortMessage;

        public TextView more;
        public TextView del;
        public NotificationViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            title = (TextView) itemView.findViewById(R.id.noti_title);
            shortMessage = (TextView) itemView.findViewById(R.id.noti_short_msg);
            more = (TextView) itemView.findViewById(R.id.noti_msg_more);
            del = (TextView) itemView.findViewById(R.id.noti_msg_del);
        }
    }
}
